<template>
  <div class="app-container">
    <!-- 会员模板查询模块 -->
    <div class="receivableSheet">
      <!-- 查询模块 -->
      <el-form ref="form"  :model="form" label-width="130px" :inline="true">
        <el-row>
          <el-col :span="6">
            <el-form-item label="模板名称">
              <el-input v-model="form.keyword" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="使用地区">
              <el-select v-model="form.province_id" placeholder="请选择" clearable>
                <el-option
                  v-for="item in options"
                  :key="item.province_id"
                  :label="item.province_name"
                  :value="item.province_id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="6">
            <!-- <el-form-item> -->
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="toRoutes('plusFormatAdd')">添加模板</el-button>
            <!-- </el-form-item> -->
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <el-table ref="tableData" :data="tableData" style="width: 100%">
        <el-table-column fixed type="index" label="编号" width="50" align="center" />
        <el-table-column prop="name" label="模板名称" align="center" />
        <el-table-column prop="province_name" label="使用地区" align="center" />
        <el-table-column label="平台卡券" align="center">
          <template slot-scope="scope">
            <!-- &emsp; -->
            <span v-for="(i,index) in scope.row.goods_tickets" :key="index">
              {{ i.type_name+" " }}&emsp;
              <br />
            </span>
          </template>
        </el-table-column>
        <el-table-column label="医美卡券" align="center">
          <template slot-scope="scope">
            <span v-for="(i,index) in scope.row.beauty_tickets" :key="index">
              {{ i.type_name }}&emsp;
              <br />
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.status | statusType }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="添加时间" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.created_at | dateFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="toRoutes('plusFormatInfo',scope.row.id)">查看</el-button>
            <el-button
              v-if="scope.row.status==1"
              size="small"
              type="text"
              @click="handleShelves(scope.row.id,2)"
            >下架</el-button>
            <el-button
              v-if="scope.row.status==2"
              size="small"
              type="text"
              @click="handleShelves(scope.row.id,1)"
            >上架</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
  <!-- </div>
  </div> -->
</template>
<script>
import { getPro, getTemList, isOn, temCount } from "@/api/plusMange";
export default {
  name: "PlusFormatList",
  filters: {
    statusType(e) {
      switch (e) {
        case 1:
          return "上架";
          break;
        case 2:
          return "下架";
          break;
        default:
          // return "暂无";
          break;
      }
    },
    // 时间戳转化
    dateFilter(e) {
      if (e != 0) {
        return new Date(Number(e) * 1000).format("yyyy-MM-dd hh:mm:ss");
      } else {
        return "";
      }
    }
  },
  data() {
    return {
      // form表单
      form: {
        keyword: "",
        province_id: "",
        page: 1,
        size: 10
      },
      // 使用地区
      options: [],
      tableLoading: true,
      // 主页列表
      tableData: [],
      newDatas: "",
      page_sizes: [10, 30, 50, 100], // 每页显示个数
      page_totals: 60 // 总条目数,
    };
  },
  inject: ["reload"],
  created() {
    this.getArea();
    this.getList(this.form);
  },
  methods: {
    // 获取模板列表
    getList(e) {
      getTemList(e)
        .then(res => {
          // console.log(res)
          if (res.data.code == 200) {
            // console.log(res);
            const resData = res.data.data;
            this.page_totals = res.data.meta.total;
            this.tableData = resData;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
          this.tableLoading = false;
        })
        .catch(err => {
          this.tableLoading = false;
          this.errorStatus(err.msg);
        });
    },
    // 获取省
    getArea() {
      getPro().then(res => {
        const resData = res.data.data;
        this.options = resData;
      });
    },
    // 路由跳转
    toRoutes(e, id) {
      const route = { name: e, query: { id: id } };
      this.$router.push(route);
    },
    // 查看模板
    // handleFormatInfo() {
    //   this.$router.push('./plusFormatInfo')
    // },
    // 立即下架
    handleShelves(id, opt) {
      const datas = { id, opt };
      if (opt == 1) {
        // 上架
        this.$confirm("确认上架该模板吗?", "上架提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            this.changeStatus(datas);
            this.$message({
              type: "success",
              message: "状态修改成功!"
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      } else if (opt == 2) {
        this.getCount({ id: id });
        this.newDatas = datas;
      }
    },
    // 获取模板的店铺数
    getCount(e) {
      temCount(e)
        .then(res => {
          // console.log(res);
          if (res.data.code == 200) {
            const count = res.data.data.count;
            // this.count = res.data.data.count;
            this.$nextTick(() => {
              this.$confirm(
                "目前已有" + count + "家商户正在使用该模板确认下架该模板吗?",
                "下架提示",
                {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning"
                }
              )
                .then(() => {
                  // this.changeStatus(datas);
                  // console.log(999, this.newDatas);
                  this.changeStatus(this.newDatas);
                  this.$message({
                    type: "success",
                    message: "状态修改成功!"
                  });
                })
                .catch(() => {
                  this.$message({
                    type: "info",
                    message: "已取消"
                  });
                });
            });
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {
          // console.log(err);
          this.errorStatus(err.msg);
        });
    },
    // 修改状态
    changeStatus(e) {
      isOn(e)
        .then(res => {
          if (res.data.code == 200) {
            // console.log(res);
            this.reload();
            this.succStatus("状态修改成功");
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {
          this.errorStatus(err.msg);
        });
    },
    // 查询
    handleSearch() {
      this.form.page = 1;
      this.getList(this.form);
    },
    // 每页显示条数
    handleSizeChange(val) {
      this.form.size = val;
      this.getList(this.form);
    },
    // 当前页
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
    // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },
    // 成功状态弹窗
    succStatus(txt, status) {
      this.eleMsg(txt, "success");
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    }
  }
};
</script>
<style scoped>
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
/* 分页样式 */
.pagesCon {
  margin-top: 20px;
  text-align: right;
}
</style>
